/*
 * ThreadSim.java
 * Responsibilities:
 * - driver of the program
 * - check for valid input arguments
 */

import java.io.*;

public class ThreadSim extends Thread {
	
	private static int numThread;
	private static boolean debug;


	private static void usageAndExit(int status) {
	    System.out.println("Usage: mode num-threads <trap|sort> <-d|debug>");
	    System.exit(status);
  	}// end usageAndExit
	
	public static void main(String args[])throws IOException {
		InputStreamReader converter = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(converter);
	
		// check for valid input argument.
		if("help".equals("args[0]"))
			usageAndExit(-1);
		if(args.length < 2 || args.length > 4) {
		      usageAndExit(-1);
		}
	
		// running in concurrent mode
		if(Integer.parseInt(args[0]) == 1) {
			
			if(args.length > 2 && args.length < 4 && args[2].equals("-d"))
				debug = true;

			
			System.out.println("Running in concurrent mode.");
			numThread = Integer.parseInt(args[1]);
			System.out.println("Run workloads with " + numThread + " thread(s).");
			// hard code inputs & run workloads serially
			
			System.out.println("Start trapaziod workload with n = 10000000...");
			Trap myTrap = new Trap(numThread, 10000000, 0, 10, debug, true);
		
			System.out.println("Start sorting workload with n = 10000...");
			Sort mySort = new Sort(numThread, 10000, debug, true);
			
			System.out.println("Start fibo workload with n = 1000...");
			Fibo myFibo = new Fibo(numThread, 1000, debug, true);
			
			
			 // do nothing, waiting for all threads to finish
			long startTime = System.currentTimeMillis();  
			long stopTime = startTime + 20000;  
		    while ( System.currentTimeMillis() < stopTime ) {  
			      
			} 
			
		    // finished all jobs and print the total execution time.
			System.out.println("Finished all the workloads.");
			System.out.println("Total execution time is: " + 
					(mySort.getExecutionTime() + myTrap.getExecutionTime()  + myFibo.getExecutionTime()) * Math.pow(10, -9) + " seconds.");
		}
		
		// running in serialize mode.
		else if(Integer.parseInt(args[0]) == 2) {
			System.out.println("Running in serialize mode.");

			if(args.length > 3 && args.length < 5 && args[3].equals("-d"))
				debug = true;
		

			numThread = Integer.parseInt(args[1]);
			if("trap".equals(args[2])) {
				int numIntegral;
				double left, right = 0;
				System.out.println("Enter the value of left interval...");
				left = Double.parseDouble(in.readLine());
				System.out.println("Enter the value right interval...");
				right = Double.parseDouble(in.readLine());
				
				// check for valid input
				if(left >= right) {
					System.out.println("Error: value of left must be greater than right.");
				}
				System.out.println("Enter the number of interval...");
				numIntegral = Integer.parseInt(in.readLine());
				
				if(numIntegral <= 0) {
					System.out.println("Error: The number of interval must be greater than zero.");
					System.exit(-1);
				}
				System.out.println("Running trapaziod rule workload with " + args[0].toString() + " thread(s).");
				Trap myTrap = new Trap(numThread, numIntegral, left, right, true, false);
				myTrap.trapazoid();
				
			}
			else if("sort".equals(args[2])) {
				System.out.println("Running sorting workload with " + args[1].toString() + " thread(s).");
				String arraySize = "";
				
				
				System.out.println("Enter size of array...");
				arraySize = in.readLine();
				
				Sort mySort = new Sort(numThread, Integer.parseInt(arraySize), true, false);
				mySort.createArray();
				mySort.sortArray();
			}
			else {
				usageAndExit(-1);
			}
		}
		else {
			usageAndExit(-1);
		}
		
		/*
		else if("fibo".equals(args[1])) {
			System.out.println("Running fibonnaci workload with " + args[0].toString() + " thread(s).");
			String n = "";
			
			System.out.println("Enter n....");
			n = in.readLine();
			Fibo myFibo = new Fibo(Integer.parseInt(args[0]), Integer.parseInt(n));
			myFibo.findFibo();
		}
		*/
		
	}// end main
}// end class ThreadSim
